# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

import json


class MyspiderPipeline(object):
    def __init__(self):
        self.file = open('itcast.json', 'w')

    def process_item(self, item, spider):
        # 将 item 对象强转成字典,该操作只能在scrapy中使用
        item=dict(item)
        # 先将字典数据序列化
        json_data = json.dumps(item, ensure_ascii=False) + ',\n'
        # 将数据写入文件
        self.file.write(json_data)

        # 默认使用完管道之后需要将数据返回给引擎
        return item

    def __del__(self):
        self.file.close()
